// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   MySqlBaseDAO.java

package net.topcrazy.base.db.dao;

import java.util.HashMap;
import java.util.Map;

import net.topcrazy.base.common.EasyGridPageObj;
import net.topcrazy.base.common.PageObj;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class MySqlBaseDAO extends BaseDAO
{

	private static final Log log = LogFactory.getLog(MySqlBaseDAO.class);

	public MySqlBaseDAO()
	{
	}

	protected Map findPageForEasyGrid(String selectSql, Object params[], EasyGridPageObj pageObj)
		throws Exception
	{
		Map result = new HashMap();
		if (log.isDebugEnabled())
		{
			log.debug("");
			log.debug((new StringBuilder("开始执行（SQL分页查询） 开始时间：")).append(getTT().getCurrentDate_YYYYMMDDHHMMSS_millisecond()).toString());
		}
		selectSql = getTT().handleBlank(selectSql);
		pageObj.setTotal(findRecordsAmountAutoHandle(selectSql, params));
		java.util.List rows = getDbTemplate().findForListWithOutLog(wrapSql(selectSql, pageObj), params);
		result.put("total", Integer.valueOf(pageObj.getTotal()));
		result.put("rows", rows);
		if (log.isDebugEnabled())
			log.debug((new StringBuilder("执行完成（SQL分页查询） 结束时间：")).append(getTT().getCurrentDate_YYYYMMDDHHMMSS_millisecond()).append(" 返回记录数：").append(result.size()).append("，总记录数：").append(pageObj.getTotal()).toString());
		return result;
	}

	private int findRecordsAmountAutoHandle(String selectSql, Object params[])
		throws Exception
	{
		getDbTemplate().testStringIsNullOrBlank(selectSql);
		return getDbTemplate().findForInteger((new StringBuilder(" select count(1) from ( ")).append(selectSql).append(" ) as _query_records_ ").toString(), params);
	}
	private String wrapSql(String selectSql, PageObj pageObj)
		throws Exception
	{
		StringBuffer sql = new StringBuffer(selectSql);
		sql.append(" limit ").append((pageObj.getPageIndex() - 1) * pageObj.getPageCount()).append(",").append(pageObj.getPageCount());
		return sql.toString();
	}

}
